package com.lanhai.longheng.dispatch;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import com.lanhai.longheng.enums.DeviceDataTypeEnum;

public class DeviceCmdMap {
	static Map<Long, String> cmdMap=Collections.synchronizedMap(new HashMap<Long, String>());
	
	/*
	 *  三种状态理论一个map就能处理，但PLC的指令并不友好
	 *  高频处理，不需要写redis
	 */
	static Map<Long, String> transMap=Collections.synchronizedMap(new HashMap<Long, String>()); 
	static Map<Long, String> leaveMap=Collections.synchronizedMap(new HashMap<Long, String>()); 
	static Map<Long, String> receiveMap=Collections.synchronizedMap(new HashMap<Long, String>());  

	public static   void save(long deviceid, String cmd) {
	    cmdMap.put(deviceid, cmd);
		 
	}

	public static   boolean isSame(long deviceid, String cmd) {
		String tmd = cmdMap.get(deviceid);
		if (tmd != null && tmd.equals(cmd)) {
			return true;
		} else {
			return false;
		}
	}
	
	//传输相关
	public static   void saveState(long deviceid, DeviceDataTypeEnum  type) {
		switch(type) {
		case AllowLeave:
			leaveMap.put(deviceid, "1");
			break;
		case AllowTrans:
			transMap.put(deviceid, "1");
			break;  
		case Receive:
			receiveMap.put(deviceid, "1");
			break;
		default:
			break;
		
		}  
	}

	/**
	 * 允许传输
	 * @param deviceid
	 * @return
	 */
	public static   boolean canTrans(long deviceid ) {
		String k=transMap.get(deviceid);
		if(k!=null&&k.length()>0) {
			return true;
		}else {
			return false;
		} 
		
	}
	
	

	/**
	 * 准备转动 
	 * @param deviceid
	 * @return
	 */
	public static   boolean canReceive(long deviceid ) {
		String k=receiveMap.get(deviceid);
		if(k!=null&&k.length()>0) {
			return true;
		}else {
			return false;
		} 
	}
	
	 

	/**
	 * 允许离开
	 * @param deviceid
	 * @return
	 */
	public static   boolean canLeave(long  deviceid) {
		String k=leaveMap.get(deviceid);
		if(k!=null&&k.length()>0) {
			return true;
		}else {
			return false;
		} 
	} 
	
	public static   void clearState(long deviceid ) { 
		leaveMap.put(deviceid, ""); 
		transMap.put(deviceid, ""); 
		receiveMap.put(deviceid, "");
	}
}
